<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%@ include file="/common/includes.jsp"%>

<jsp:useBean id="nowDate" class="java.util.Date" />
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title>管理编辑</title>
<!-- import css and js -->
<%@ include file="/common/metaInput.jsp"%>
<script src="${ctx}/js/jsp/comm.js" type="text/javascript"></script>
<script type="text/javascript"
	src="${ctx}/js/My97DatePicker/WdatePicker.js"></script>
	<script type="text/javascript" src="${ctx}/js/jsp/yd/logic/ItemStateData.js"></script>
	<script src="${ctx}/js/jsp/date.js" type="text/javascript"></script>
<script type="text/javascript">
	var basic_Url = "${ctx}/yd/logic/soware";
	var grid;
	var grid2;
	var ItemStateList = ItemStateData.Rows;//物料等级

	$(function() {
		//表格结构
		
		
		var griddata = {};
		//普通出库表格
		grid = $("#table1").ligerGrid({
			columns : [ 
		{ display: '操作',width: 30,
		    render: function (record, rowindex, value, column) {
		    	var rc = JSON.stringify(record);
			  	  if(record.openBatch=='Y'){
			  		 return "<a href='javascript:ItemWindow("+rc+","+rowindex+")'><img src='${ctx}/js/ligerUI/skins/icons/add.gif' alt='添加' /></a>";
			  	  }else{
			  		 return "<h1>+</h1>";
			  	  }
				  }},
			{display : 'ID',name : 'id',width : 1,hide : true
			},{display : 'sourceBillType',name : 'sourceBillType',width : 1,hide : true
			}, {display : '产品编码',name : 'code',width : 1,hide:true,//不保存
				render:function(item){
					item.code=item.prodNo; 
					return item.code;
				}
			}, {display : '<font color="gray">产品编码</font>',name : 'prodNo',width : 100,
// 				render:function(item){
// 					//item.code=item.prodNo; 
// 					return item.prodNo;
// 				}
			render: function (record, rowindex, value, column) {
				var rc = JSON.stringify(record);
				  if(record.openBatch=='Y'){
					  return "<a href='javascript:ItemWindow("+rc+","+rowindex+")'>"+record.prodNo+"</a>";
				  	  }else{
				  		 return record.prodNo;
				  	  }
			  	  
			  }
			}, {display : '<font color="gray">产品名称</font>', name : 'prodName',width : 80
			},{display : '<font color="gray">型号规格</font>',name : 'prodSpecDesc',width : 130},
			{ display: '<font color="gray">物料是否可用批次</font>', name: 'openBatch' , width: 150,hide:true ,render: function (item, rowindex, value, column)
			    {
        	   	if(item.openBatch=="Y"){
        	   		return "可用";
        	   	}else{
        	   		return "不可用";
        	   	}
		   		
	    }},
			{display : '<font color="gray">基本单位</font>', name : 'prodBUnit',width : 80
			},{display : '<font color="gray">单位换算率</font>',name : 'prodUnitConvert',width : 80,render:function(item){
				changequantity(item);
				return item.prodUnitConvert;
			}
			},{display : '单位<font color="red">*</fon	t>',name : 'prodUnit',width : 50,editor : {
	          	  type:'selecturl',dataItem:'dataItem',path:'${ctx}/yd/basic/measurementUnit/unitlist.do',valueColumnName: 'unitname',displayColumnName: 'unitname', displayColumnName: 'unitname',onChanged:function(record,value,column,rowindex,datagrid){
	            	  grid.updateCell('itemBatch', '', record);
	            	  grid.updateCell('prodSfquantity', '', record);
	            	  grid.updateCell('val', '', record);
	            	  grid.updateCell('prodBSfquantity', '', record);
	            	  grid.updateRow(record);
	            	  }}, render: function (item)
	                   {
	        	 		  var dataitem = $.parseJSON($("#dataItem").val());
	        	 		  if(dataitem!=null){
	        	 			 for (var i = 0; i < dataitem.length; i++)
	                         {
	                             if (dataitem[i]['unitname'] == item.prodUnit&&dataitem[i]['materialscode']==item.prodNo){
	                           	  	 grid.updateCell('prodUnitConvert', dataitem[i]['unitconvert'], item);
	                           	  	 return dataitem[i]['unitname'];
	                                
	                             } 
	                         }
	        	 		  }
	                       return item.prodUnit;
	                   }
// 			},{display : '产品批次<font color="red">*</font>',name : 'prodBatch',width : 90, isSort: false
// 	 			//<c:if test="${'ADD' eq flag or '0'eq soware.billstatus or '3'eq soware.billstatus}">
// 	 			,editor: { /*onChanged:getkcQuantity,*/type: 'selectbatch',whcode:'whcode', databatch:'batchItem',path:'${ctx}/yd/logic/otherOutwh/getWhBatch.do', valueColumnName: 'batch', displayColumnName: 'batch',
// 		   			ext :
// 		                function (rowdata)
// 		                {
// 		                    return {
// 		                        onBeforeOpen: function(){
// 		                        	if(undefined==$("#whcode").val() || ""==$("#whcode").val()){
// 		                    			$.dialog.alert("请先选择仓库");
// 		                    			return ;
// 		                    		}
		                        	
// 		                        	getDG().dialog({
// 							    		title:'选择批次',
// 							    		width : 300,
// 										height : 350,
// 										content: 'url:' + sys_context+'/public/windowalert/itembatch.do?whCode=' + $("#whcode").val()+'&maCode='+rowdata.prodNo,//
// 							    		lock : true,
// 							    		max : true,
// 							    		min : true,
// 							    		ok: function(){
// 							    			var rows = this.content.getSelected();
// 											if (rows != null) {
// 												grid.updateCell('prodBatch', rows[0].batch, rowdata);
// 												grid.endEdit();
// 												getBatchkcQuantity(rowdata);//修好后的触发
// 											}
// 											this.content.Close();
// 							        		return false;
// 							    	    },
// 							    	    cancelVal: '关闭',
// 							    	    cancel: function(){
// 							    	    	grid.endEdit();
// 							    	    } 
// 							    	}).zindex();
// 		                        }, render: function ()
// 		                        { 
// 		                        	var batchItem = $.parseJSON($("#batchItem").val());
// 		            		  		if(batchItem!=null){
// 		            			 	for (var i = 0; i < batchItem.length; i++)
// 		                     			{
// 		                         			if (batchItem[i]['batch'] == rowdata.prodBatch){
// 		                            	 		return batchItem[i]['batch'];
// 		                         			} 
// 		                     			}
// 		            		  		}
// 		                        }

// 		                    };
// 		                }
// 					 },
// 		   		render: function (item){
// 	   		 		var batchItem = $.parseJSON($("#batchItem").val());
// 			  		if(undefined != batchItem && "" != batchItem){
// 				 	for (var i = 0; i < batchItem.length; i++)
// 	         			{
// 	             			if (batchItem[i]['batch'] == item.prodBatch){
// 	                	 		return batchItem[i]['batch'];
// 	             			} 
// 	         			}
// 			  		}
// 	                return item.prodBatch;
// 	            }
// 				//</c:if>
 			}, { display: '等级<font color="red">*</font>', name: 'status', width: 60
				,render: function (item){
					 var state=item.status;
	           		  if(state=="1"){
	           			  return "一等品";
	           		  }
	           		  if(state=="2"){
	           			  return "次品";
	           		  }
	           		  if(state=="3"){
	           			  return "旧件";
	           		  }
					}
			},
			
// 			},  {display : '发货仓位',name : 'hubcode',width : 80,editor: { type: 'text'}
// 			},  {display : '发货仓位名称',name : 'hubname',width : 80,editor: { type: 'text'}
			
			
			{display : '可用库存基本单位数量',name : 'kyBQuantity',width : 1,hide : true
			},{display : '<font color="gray">可用库存数量</font>',name : 'kyQuantity',width : 100
			},{display : '<font color="gray">本单可出库数量</font>',name : 'tmpCanCkQuantity',width : 100
				,render:function(item){
					item.tmpCanCkQuantity = item.prodQuantity - item.ckQuantity - item.frozenQuantity;
					return item.tmpCanCkQuantity+"";
				  }
			},{display : '本次实发数量<font color="red">*</font>',name : 'prodSfquantity',width : 90
				//<c:if test="${'ADD' eq flag or '0'eq soware.billstatus or '3'eq soware.billstatus}">
				,editor : {type : 'float'},
				render:function(item){
					if("Y"==item.openBatch){
						item.prodSfquantity=item.val;
					}else{
						dealProductQuantity(item);
					}
					return item.prodSfquantity;
			  	}
				//</c:if>
			},{display : '实发基本单位数量',name : 'prodBSfquantity',width : 120
			},{display : '保质期（天）',name : 'expirationDay',width : 80
				//<c:if test="${'ADD' eq flag or '0'eq soware.billstatus or '3'eq soware.billstatus}">
				,editor : {type : 'int'}
				//</c:if>
			},{display : '有效期',name : 'validityDate',width : 100,type:'date',format:'yyyy-MM-dd'
				//<c:if test="${'ADD' eq flag or '0'eq soware.billstatus or '3'eq soware.billstatus}">
				,editor : {type : 'scdate'}/*,
				render: function (item){
					if(undefined != item.validityDate && ""!=item.validityDate){
						//var cdate=new Date(item.validityDate);
		          		  //return formatDate(cdate,"yyyy-MM-dd");
						return new Date(item.validityDate).formatdt('yyyy-MM-dd');
					}
          	  	}*/
				//</c:if>
          	  , render: function (item)
				{
        	  if(item.validityDate!=''&& item.validityDate!=null){
        		  item.validityDate=formatDate(new Date(item.validityDate),"yyyy-MM-dd");
        	  }
					return item.validityDate;	
			}
			},{display : '<font color="gray">本单已退数量</font>',name : 'ytQuantity',width : 80
			},{display : '已退基本单位数量',name : 'ytBQuantity',width : 1,hide : true
			},{display : '<font color="gray">订单基本单位数量</font>',name : 'prodBQuantity',width : 1,hide : true
			},{display : '<font color="gray">订单数量</font>',name : 'prodQuantity',width : 80
			},{display : '已出库基本单位数量',name : 'ckBQuantity',width : 1,hide : true
			},{display : '<font color="gray">订单已出库数量</font>',name : 'ckQuantity',width : 100
			},{display : '<font color="gray">订单已冻结数量</font>',name : 'frozenQuantity',width : 100
			},{display : '已冻结数量基本单位数量',name : 'frozenBQuantity',width : 1,hide : true
			
// 			},{display : '单位成本<font color="red">*</font>',name : 'prodPerCost',width : 80,editor : {type : 'float'},
// 				render:function(item){
// 				  return item.prodPerCost;
// 			  }
// 			},{display : '成本',name : 'prodCost',width : 80
			
			
			}, {display : '<font color="gray">销售含税单价(基本单位)</font>',name : 'prodPriceWithTax',width : 150
			}, {display : '销售单价',name : 'prodPrice',width : 1,hide:true
			}, {display : '销售金额',name : 'prodSum',width : 1,hide:true
			}, {display : '备注',name : 'remark',width : 100
				//<c:if test="${'ADD' eq flag or '0'eq soware.billstatus or '3'eq soware.billstatus}">
				,editor: { type: 'text'}
				//</c:if>
			},
			{ display: '<font color="gray">值</font>', name: 'val' ,width:1,hide:1},
			{display : '批次明细',name : 'itemBatch',width : 1,hide:true
			}
// 			,{ display: '分多批操作',width: 150,
//       	        render: function (record, rowindex, value, column) {
//        		  	   return "<a href='javascript:manyBatch(" + rowindex + ")'>分多批次</a>";
//        		  }}
			],
			enabledEdit : true,
			usePager : true,
			pageSize : 1000,
			pageSizeOptions : [ 1000 ],
			//data : griddata,
			parms:[{ name : 'id' , value : '${soware.id}'},{ name : 'type' , value : 'grid1'}],
            url : basic_Url+"/griditems.do",
			width : "100%",
			height : 200,
			checkbox : true
			,toolbar : {
				items : [ {
					text : '<b>普通出库</b>'
				}
				//<c:if test="${'ADD' eq flag or '0'eq soware.billstatus or '3'eq soware.billstatus}">
				,{line : true}, {
					text : '删除',
					click : deleteRow,
					img : '${ctx}/js/ligerUI/skins/icons/delete.gif'
				} 
				//</c:if>
				]
			}
			
		});
		
		//冻结出库表格
		grid2 = $("#table2").ligerGrid({
			columns : [ 
		{ display: '操作',width: 30,
		    render: function (record, rowindex, value, column) {
		    	var rc = JSON.stringify(record);
			  	  if(record.openBatch=='Y'){
			  		 return "<a href='javascript:ItemFreezedealWindow("+rc+","+rowindex+")'><img src='${ctx}/js/ligerUI/skins/icons/add.gif' alt='添加' /></a>";
			  	  }else{
			  		 return "<h1>+</h1>";
			  	  }
				  }
		  	 },
			 {display : 'ID',name : 'id',width : 1,hide : true
			},{display : 'sourceBillType',name : 'sourceBillType',width : 1,hide : true
			}, {display : '<font color="gray">产品编码',name : 'code',width : 1,hide:true,//不保存
				render:function(item){
					item.code=item.prodNo; 
					return item.code;
				}
			}, {display : '<font color="gray">产品编码</font>',name : 'prodNo',width : 100,
				render: function (record, rowindex, value, column) {
					var rc = JSON.stringify(record);
					  if(record.openBatch=='Y'){
						  return "<a href='javascript:ItemFreezedealWindow("+rc+","+rowindex+")'>"+record.prodNo+"</a>";
					  	  }else{
					  		 return record.prodNo;
					  	  }
					  }
			}, {display : '<font color="gray">产品名称</font>', name : 'prodName',width : 100
			},{display : '<font color="gray">型号规格</font>',name : 'prodSpecDesc',width : 130
			},
			{ display: '<font color="gray">物料是否可用批次</font>', name: 'openBatch' ,hide:true,width: 150, render: function (item, rowindex, value, column)
			    {
        	   	if(item.openBatch=="Y"){
        	   		return "可用";
        	   	}else{
        	   		return "不可用";
        	   	}
		   		
	    }},
			{display : '<font color="gray">仓库编码</font>',name : 'whcode',width : 128
			},{display : '<font color="gray">仓库名称</font>',name : 'whname',width : 120
			},
// 			{display : '产品批次<font color="red">*</font>',name : 'prodBatch',width : 90, isSort: false
// 			}, 
			{ display: '等级<font color="red">*</font>', name: 'status', width: 60,
				 render: function (item){
					 var state=item.status;
           		  if(state=="1"){
           			  return "一等品";
           		  }
           		  if(state=="2"){
           			  return "次品";
           		  }
           		  if(state=="3"){
           			  return "旧件";
           		  }
				}
			},
			{display : '<font color="gray">基本单位</font>', name : 'prodBUnit',width : 80
			},{display : '<font color="gray">单位换算率</font>',name : 'prodUnitConvert',width : 80 
			},{display : '<font color="gray">单位</font>',name : 'prodUnit',width : 50
			},{display : '<font color="gray">本次可出库数量</font>',name : 'tmpCanCkQuantity',width : 100
				,render:function(item){
					item.tmpCanCkQuantity = item.frozenQuantity;
					return item.tmpCanCkQuantity+"";
				  }
			},{display : '本次实发数量<font color="red">*</font>',name : 'prodSfquantity',width : 90
				//<c:if test="${'ADD' eq flag or '0'eq soware.billstatus or '3'eq soware.billstatus}">
				,editor : {type : 'float'},
				render:function(item){
					if(item.openBatch=="Y"){
						item.prodSfquantity=item.val;
					}else{
						dealProductQuantity(item);
					}
				  
				  return item.prodSfquantity;
			  	}
				//</c:if>
			},{display : '本次实发基本单位数量',name : 'prodBSfquantity',width : 120
			},{display : '保质期（天）',name : 'expirationDay',width : 80
				//<c:if test="${'ADD' eq flag or '0'eq soware.billstatus or '3'eq soware.billstatus}">
				,editor : {type : 'int'}
				//</c:if>
			},{display : '有效期',name : 'validityDate',width : 100,type:'date',format:'yyyy-MM-dd'
				//<c:if test="${'ADD' eq flag or '0'eq soware.billstatus or '3'eq soware.billstatus}">
				,editor : {type : 'scdate'}/*,
				render: function (item){
					if(undefined != item.validityDate && ""!=item.validityDate){
						//var cdate=new Date(item.validityDate);
		          		  //return formatDate(cdate,"yyyy-MM-dd");
						return new Date(item.validityDate).formatdt('yyyy-MM-dd');
					}
          	  	}*/
				//</c:if>
          	  , render: function (item)
				{
      	  if(item.validityDate!=''&& item.validityDate!=null){
      		  item.validityDate=formatDate(new Date(item.validityDate),"yyyy-MM-dd");
      	  }
					return item.validityDate;	
			}
			},{display : '<font color="gray">本单已退数量</font>',name : 'ytQuantity',width : 80
			},{display : '已退基本单位数量',name : 'ytBQuantity',width : 1,hide : true
			},{display : '<font color="gray">订单基本单位数量</font>',name : 'prodBQuantity',width : 1,hide : true
			},{display : '<font color="gray">订单数量</font>',name : 'prodQuantity',width : 80
			},{display : '<font color="gray">订单已冻结数量</font>',name : 'frozenQuantity',width : 100
			},{display : '已冻结数量基本单位数量',name : 'frozenBQuantity',width : 1,hide : true
			},{display : '可用库存基本单位数量',name : 'kyBQuantity',width : 1,hide : true
			},{display : '<font color="gray">可用库存数量</font>',name : 'kyQuantity',width : 1,hide : true
			},{display : '已出库基本单位数量',name : 'ckBQuantity',width : 1,hide : true
			},{display : '<font color="gray">已出库数量</font>',name : 'ckQuantity',width : 120
			
			}, {display : '<font color="gray">销售含税单价(基本单位)</font>',name : 'prodPriceWithTax',width : 150
			}, {display : '销售单价',name : 'prodPrice',width : 1,hide:true
			}, {display : '销售金额',name : 'prodSum',width : 1,hide:true
			},{display : '<font color="gray">源单号</font>',name : 'sourceBillNo',width : 125
			}, {display : '批次明细',name : 'itemBatch',width:1,hide:true
			}, {display : '修改状态',name : 'updstatus',width : 1,hide:true
			}, {display : '值',name : 'val',width : 1,hide:true
			},{display : '备注',name : 'remark',width : 100
				//<c:if test="${'ADD' eq flag or '0'eq soware.billstatus or '3'eq soware.billstatus}">
				,editor: { type: 'text'}
				//</c:if>
			}],
			enabledEdit : true,
			usePager : true,
			pageSize : 1000,
			pageSizeOptions : [ 1000 ],
			//data : {},
			parms:[{ name : 'id' , value : '${soware.id}'},{name : 'type' , value : 'grid2'}],
            url : basic_Url+"/griditems.do",
			width : "100%",
			height : 200,
			checkbox : true,
			toolbar : {
				items : [ {
					text : '<b>冻结出库</b>'
				}
				//<c:if test="${'ADD' eq flag or '0'eq soware.billstatus or '3'eq soware.billstatus}">
				,{line : true}, {
					text : '删除',
					click : function(){grid2.deleteSelectedRow();},
					img : '${ctx}/js/ligerUI/skins/icons/delete.gif'
				} 
				//</c:if>
				]
			}
		});
		
		//表格数据加载--普通出库
		/* var manager = $("#table1").ligerGetGridManager();
		//<c:forEach items='${sowareitem1}' var='item'>
		manager.addRow2({
			id : "${item.id}",
			sourceBillType:"${item.sourceBillType}",
			prodNo : "${item.prodNo}",
			prodName : "${item.prodName}",
// 			brand : "\${item.brand}",
			prodBUnit:'${item.prodBUnit}',
			prodUnitConvert:'${item.prodUnitConvert}',
			prodUnit : "${item.prodUnit}",
			prodPrice : "${item.prodPrice}",
			prodPriceWithTax : "${item.prodPriceWithTax}",
			prodSum : "${item.prodSum}",
// 			prodBatch : "${item.prodBatch}",
			status : "${item.status}",
			
			prodBQuantity : "${item.prodBQuantity}",
			prodQuantity : "${item.prodQuantity}",
			
			ckBQuantity : "${item.ckBQuantity}",
			ckQuantity : "${item.ckQuantity}",
			ytBQuantity : "${item.ytBQuantity}",
			ytQuantity : "${item.ytQuantity}",
			
			kyBQuantity : "${item.kyBQuantity}",
			kyQuantity : "${item.kyQuantity}",
 			prodBSfquantity : "${item.prodBSfquantity}",
 			prodSfquantity : "${item.prodSfquantity}",//"<fmt:formatNumber value="${item.prodSfquantity}" pattern="#.#####"/>",
//  			prodPerCost : "${item.prodPerCost}",
//  			prodCost : "${item.prodCost}",
 			
 			frozenQuantity:"${item.frozenQuantity}",
 			frozenBQuantity:"${item.frozenBQuantity}",
 			
 			expirationDay : "${item.expirationDay}",
 			validityDate : ("true"=="${not empty item.validityDate}"?new Date("${item.validityDate}").formatdt('yyyy-MM-dd'):""),
 			
			prodSpecDesc : "${item.prodSpecDesc}",
			itemBatch : '${item.itemBatch}',
			remark : "${item.remark}",
			openBatch: "${item.openBatch}",
    		val : "${item.val}"
		});
		//</c:forEach> */
		
		//表格数据加载--冻结出库
		/* var manager2 = $("#table2").ligerGetGridManager();
		//<c:forEach items='${sowareitem2}' var='item'>
		manager2.addRow2({
			id : "${item.id}",
			sourceBillType:"${item.sourceBillType}",
			sourceBillNo:"${item.sourceBillNo}",
			whcode:"${item.whcode}",
			whname:"${item.whname}",
			prodNo : "${item.prodNo}",
			prodName : "${item.prodName}",
			prodBUnit:'${item.prodBUnit}',
			prodUnitConvert:'${item.prodUnitConvert}',
			prodUnit : "${item.prodUnit}",
			prodPrice : "${item.prodPrice}",
			prodPriceWithTax : "${item.prodPriceWithTax}",
			prodSum : "${item.prodSum}",
// 			prodBatch : "${item.prodBatch}",
			status : "${item.status}",
			
			prodBQuantity : "${item.prodBQuantity}",
			prodQuantity : "${item.prodQuantity}",
			
			ckBQuantity : "${item.ckBQuantity}",
			ckQuantity : "${item.ckQuantity}",
			ytBQuantity : "${item.ytBQuantity}",
			ytQuantity : "${item.ytQuantity}",
			
			kyBQuantity : "${item.kyBQuantity}",
			kyQuantity : "${item.kyQuantity}",
 			prodBSfquantity : "${item.prodBSfquantity}",
 			prodSfquantity : "${item.prodSfquantity}",
//  			prodPerCost : "${item.prodPerCost}",
//  			prodCost : "${item.prodCost}",
 			
 			frozenQuantity:"${item.frozenQuantity}",
 			frozenBQuantity:"${item.frozenBQuantity}",
 			
 			expirationDay : "${item.expirationDay}",
 			validityDate : ("true"=="${not empty item.validityDate}"?new Date("${item.validityDate}").formatdt('yyyy-MM-dd'):""),
 			
			prodSpecDesc : "${item.prodSpecDesc}",
// 			sourceBillNo:"${item.sourceBillNo}",
			itemBatch : '${item.itemBatch}',
			updstatus : '${item.updstatus}',
			remark : "${item.remark}",
			openBatch: "${item.openBatch}",
    		val : "${item.val}"
		});
		//</c:forEach> */
		
		//验证表单
		$("#inputForm").validate({
			onsubmit : true,
			/*执行ajaxsubmit  */
			submitHandler : function(editform) {
				try {
					url = basic_Url + "/save.do";
					var api = frameElement.api;
					var W = api.opener;
					getReturnMessage(url, "inputForm", W.searchAP);
				} catch (e) {
					alert(e);
				}
			}
		});
		
		//按钮处理
		/* btn status */
		$("[id^='btn_fb_']").hide();
		var curStatus = "${soware.billstatus}";//当前所在环节、状态
		if(curStatus == ''){//建单
			$("#btn_fb_save").show();
		}else {
			$("#btn_fb_exportExcel").show();
			formComponentDisabled(true);//禁用控件
			if (curStatus == '0'||curStatus == '3'){//草稿状态-提交二级
				if("${user.name}"=="${soware.dcruser}"){
					$("#btn_fb_approve_2").show();
					//$("#btn_fb_submit").show();
				}
				formComponentDisabled(false);//启用控件
			}else if (curStatus == '1'){//二级审批中状态
				$("#btn_fb_rebut_2").show();
				$("#btn_fb_end_2").show();
			}else if (curStatus == '2'){//二级审批完结
				
			}else{
			}
		}

		//添加模式隐藏DIV
		if("${flag}" =="ADD"){
			$("#tr1").hide();
			
		}else{
			//$("#tr2").hide();
		}
		
		//页面加载标识隐藏
		$("#pageloading").hide();
		
		//=============================
			
		//仓库下拉改变触发
		$("#storage").change(function(){ 
			var data = grid.getData();
			var code="";
			if (data.length == 0) {
// 				$.dialog.alert("请选择产品");
// 				return false;
			}else{
				for(var i=0;i<data.length;i++){
					if(i==0){
						code=data[i].code;
					}else{
						code+=","+data[i].code;
					}
				}
			}
			  $("#whcode").val($("#storage").val());
			  $("#whname").val($("#storage").find("option:selected").text());
			  
			  //表格中批次、等级、可用库存
			  var len = grid.rows.length;
			  for(var i=0 ; i<len;i++){
				  var rowData = grid.rows[i];
				  //rowData.prodBatch="";
				  //grid.updateCell('prodBatch',rowData.kyQuantity,rowData);
				  grid.updateRow(rowData,{prodBatch:""});
				  grid.updateRow(rowData,{itemBatch:""});
				  if(rowData.openBatch!="Y"){
					  var s = grid.rows[i];
		  				if((s.status!=null)&&(undefined!=$("#whcode").val() && ""!=$("#whcode").val())){
								$.ajax({
					     		      url: "${ctx}/yd/logic/otherOutwh/getStorageQuaitity.do",
					     		      global: false,
					     		      type: "POST",
					     		      dataType: "json",
					     		      data: ({
					     		    	maCode : s.code,
					     				whCode : $("#whcode").val(),
					     				hubCode : "",
					     				status : s.status,
					     				batch : "NoBatch"
					     		      }),		     
					     		      success: function(msg){
					     		    	s.kyBQuantity = msg;
					     		    	s.auto = true;
					     	   			grid.updateCell('kyBQuantity',msg,s);
					     	   			if(!isNaN(s.prodUnitConvert)){
					     	   			s.kyQuantity = (s.kyBQuantity / s.prodUnitConvert);
					 		    			grid.updateCell('kyQuantity',s.kyQuantity,s);
					     	   			}
					     		      }
					     	   });
							}
	    			  }
			  }
// 			  var len2 = grid2.rows.length;
// 			  for(var i=0 ; i<len2;i++){
// 				  var rowData2 = grid2.rows[i];
// // 				  //rowData.prodBatch="";
// // 				  //grid.updateCell('prodBatch',rowData.kyQuantity,rowData);
// // 				  grid.updateRow(rowData,{itemBatch:""});
// 			  }
			  
		}); 

		
		//销售订单的值改变触发 //
		$("#soNo").change(function(){
			//<c:forEach items='${solist}' var='state'>
        	if($("#soNo").find("option:selected").text()=='${state.no}'){
        		$("#cliName").val('${state.cliName}');
        		$("#cliNo").val('${state.cliNo}');
        		$("#cliAddr").val('${state.cliAddr}');
        		$("#currency").val('${state.currency}');
        		$("#exchangeRate").val('${state.exchangeRate}');
        		$("#deliveryDate").val('<fmt:formatDate value="${state.deliveryDate}" pattern="yyyy-MM-dd" type="both" />');
        	}
       		//</c:forEach>
       		//根据订单编号获得订单的产品item
			multipleSearch("#table1",true,"",basic_Url+"/soitemlist.do?soNo="+$("#soNo").val());
			multipleSearch("#table2",true,"",basic_Url+"/sofreezeitemlist.do?soNo="+$("#soNo").val());
		}); 
	});


	
	function changequantity(item){
		if(undefined == item){
	   		return;
	   	}
	   	if(!isNaN(item.prodBQuantity) && item.prodBQuantity!=null&&item.prodUnitConvert!=null&&item.prodUnitConvert!=''){
	   		item.prodQuantity = item.prodBQuantity/parseFloat(item.prodUnitConvert) ;
	   	}else{
	   		item.prodQuantity=null;//清除值
	   	}
	   	if(!isNaN(item.ckBQuantity) && item.ckBQuantity!=null&&item.prodUnitConvert!=null&&item.prodUnitConvert!=''){
	   		item.ckQuantity = item.ckBQuantity/parseFloat(item.prodUnitConvert) ;
	   	}
	   	if(!isNaN(item.frozenBQuantity) && item.frozenBQuantity!=null&&item.prodUnitConvert!=null&&item.prodUnitConvert!=''){
	   		item.frozenQuantity = item.frozenBQuantity/parseFloat(item.prodUnitConvert) ;
	   	}
	}
	
	function submitForm(param) {
		//设置ajax全局同步
		$("#paramflag").val(param);
		
		var data = grid.getData();
// 		if (data.length == 0) {
// 			$.dialog.alert("请选择产品");
// 			return false;
// 		}
		var ba = true;
		
		if (data != null) {
			var j = 0;
			if (param == 1 || param == 0) {
				$(data).each(function() {
					j++;
					
// 					if(undefined == this.prodBatch
// 							|| ""== this.prodBatch){
// 						$.dialog.alert("普通出库表格，第" + j + "行的“产品批次”数据不存在");
// 						ba = false;
// 						return false;
// 					}
					if(this.openBatch!="Y"){
						if (undefined == this.prodSfquantity
								|| isNaN(this.prodSfquantity)
								|| this.prodSfquantity <= 0) {
							$.dialog.alert("普通出库表格，第" + j + "行的“本次实发数量”数据不合法");
							ba = false;
							return false;
						}
						
						if (parseFloat(this.prodSfquantity) > parseFloat(this.kyQuantity) ) {
							$.dialog.alert("普通出库表格，第" + j + "行的“本次实发数量”大于“可用库存数量”");
							ba = false;
							return false;
						}
						if (parseFloat(this.prodSfquantity) > parseFloat(this.tmpCanCkQuantity)/* parseFloat(this.prodQuantity - this.ckQuantity) */) {
							$.dialog.alert("普通出库表格，第" + j + "行的“本次实发数量”大于“本单可出库数量”");
							ba = false;
							return false;
						}
					}
					
				});
			}
// 			for ( var i = 0; i < data.length; i++) {
// 				var num =parseFloat(data[i].prodSfquantity);
// 				for ( var j = 0; j < data.length; j++) {
// 					if(i != j){
// 						if(data[i].prodNo==data[j].prodNo && data[i].prodBatch==data[j].prodBatch && data[i].status==data[j].status){
// 							ba = false;
// 							$.dialog.alert("普通出库表格,物料编码为:" + data[j].prodNo + "的第"+(i+1)+"行与第"+(j+1)+"行存在相同的批次与等级!");
// 							return false;
// 						}else if(data[i].prodNo==data[j].prodNo){
// 							num +=parseFloat(data[j].prodSfquantity);
// 							if(num>parseFloat(data[i].tmpCanCkQuantity)){
// 								ba = false;
// 								$.dialog.alert("普通出库表格,物料编码为:" + data[j].prodNo + "的“本次实发数量”大于“本单可出库数量”");
// 								return false;
// 							}
// 						}
// 					}
// 				}
// 			}
		}
		
		var data2 = grid2.getData();
		
		if(undefined != data2 && null != data2 && data2.length > 0){
			var k = 0;
			$(data2).each(function() {
				k++;
				
				if(this.whcode!=$("#whcode").val()){
					$.dialog.alert("冻结出库表格，第" + k + "行的冻结出库不合法，与所选仓库不一致");
					ba = false;
					return false;
				}
				if(this.openBatch!="Y"){
				if (undefined == this.prodSfquantity
						|| isNaN(this.prodSfquantity)
						|| this.prodSfquantity <= 0) {
					$.dialog.alert("冻结出库表格，第" + k + "行的“本次实发数量”数据不合法");
					ba = false;
					return false;
				}
				
				if (parseFloat(this.prodSfquantity) > parseFloat(this.tmpCanCkQuantity)/* parseFloat(this.prodQuantity - this.ckQuantity) */) {
					$.dialog.alert("冻结出库表格，第" + k + "行的“本次实发数量”大于“本单可出库数量”");
					ba = false;
					return false;
				}
				}
			});
		}

		jQuery.ajaxSettings.async = false;
		if (ba) {
// 			$("#yfitemdata").val(JSON.stringify(datayf));
			$("#itemdata").val(JSON.stringify(data));
			$("#itemdata2").val(JSON.stringify(data2));
			$("#paramflag").val(param);
			//提交前启用控件
		  	formComponentDisabled(false);
			$("#inputForm").submit();
		}
	}

	function tableSubmit(msg,param){
		$.ligerDialog.confirm(msg, 
				function (flag) { 
				if(flag){
					submitForm(param);
					}
			}); 
	}

	function submitFormClose(param){
		tableSubmit('是否确认关闭单据操作？',param);
	}

	function submitFormEnd(param){
		tableSubmit('是否确认审批完结操作？',param);
	}

	function submitFormRebut(param){
		tableSubmit('是否确认驳回操作？',param);
	}
	
	/**
	 * 获取库存数量
	 */
    function getkcQuantity(e){
    	var rowData=grid.getRow($(e).parent()[0]);
    	getBatchkcQuantity(rowData);
    }
	
    /**
	 * 获取库存数量2
	 */
	function getBatchkcQuantity(rowData){
		if(undefined==$("#whcode").val() || ""==$("#whcode").val()){
			$.dialog.alert("请先选择仓库");
			return ;
		}
		if (rowData.prodBatch){
   		 $.ajax({
     		      url: "${ctx}/yd/logic/otherOutwh/getStorageQuaitity.do",
     		      global: false,
     		      type: "POST",
     		      dataType: "json",
     		      data: ({
     		    	maCode : rowData.code,
     				whCode : $("#whcode").val(),
     				hubCode : "",
     				status : rowData.status,
     				batch : rowData.prodBatch
     		      }),		     
     		      success: function(msg){
     		    	rowData.kyBQuantity = msg;
     		    	rowData.auto = true;
     	   			grid.updateCell('kyBQuantity',msg,rowData);
     	   			if(!isNaN(rowData.prodUnitConvert)){
 		    			rowData.kyQuantity = (rowData.kyBQuantity / rowData.prodUnitConvert);
 		    			grid.updateCell('kyQuantity',rowData.kyQuantity,rowData);
     	   			}
     	   			
     		      }
     	   });
  		}else{
  			$.dialog.alert("请先选择批次");
  		}
	}
	
	function deleteRow() {
		grid.deleteSelectedRow();
	}

	function exportExcel() {
		var id =$("#id").val();
		if(id!=""){
			var path = basic_Url + "/exportExcel.do?id="+id;
			window.open(path, "_blank");
		}else{
			$.dialog.alert("获取数据出现未知问题，请重试");
		}
	}

    /**
     * 产品的数量计算
     * @param item
     */
    function dealProductQuantity(item){
    	if(undefined == item){
    		return;
    	}
    	
    	//计算
    	//计算基本单位数量 parseFloat()
    	if(!isNaN(item.prodSfquantity) && !isNaN(item.prodUnitConvert)){
    		item.prodBSfquantity = item.prodSfquantity*item.prodUnitConvert ;
    	}else{
    		item.prodBSfquantity=null;//清除值
    	}
    	
//     	if(!isNaN(item.prodPerCost) && !isNaN(item.prodPerCost)){
//     		item.prodCost = item.prodPerCost*item.prodUnitConvert ;
//     	}else{
//     		item.prodCost=null;//清除值
//     	}
    	
    }
    
//	//驳回操作
// 	function bh(){
// 		 $.ajax({
// 				url:basic_Url+"/bh.do",  
// 	            data:{id:$("#id").val()},  
// 	            error:function(){  
// 	            	$.dialog.alert("获取销售出库单信息出错!!!");  
// 	            },  
// 	            success:function(msg){
// 	            	$.dialog.alert(msg);
// 	            	var api = frameElement.api;
// 					var W = api.opener;
// 					 W.searchAP();
// 					 CloseDialog();
// 	            }  
// 			});
// 	}
// function manyBatch(index){
// 	var data= grid.getData();
// 	var manager = $("#table1").ligerGetGridManager();
// 	manager.addRow2(data[index]);
// }
function ItemWindow(rc,index) {
 	if(undefined==$("#whcode").val() || ""==$("#whcode").val()){
			$.dialog.alert("请先选择仓库");
			return ;
	}
 	var manager = $("#table1").ligerGetGridManager();
		getDG().dialog(
				{
					title : '批次管理',
					width : 650,
					height : 350,
					content : 'url:' + sys_context+ '/yd/logic/soware/itemManager.do?data='+encodeURIComponent(JSON.stringify(manager.getRow(rc.__index)))+'&billstatus=${soware.billstatus}'+'&whcode='+$("#whcode").val(),//
					lock : true,
					max : true,
					min : true,
					ok : function() {
						this.content.yz();
						if(this.content.ba){
						var dt = this.content.grid.getData();
						if (dt != null) {
							rc.prodBSfquantity=this.content.jnum;
							rc.prodSfquantity=this.content.num;
							rc.val=this.content.num;
							rc.kyBQuantity = this.content.kyBQuantity;
							rc.kyQuantity = this.content.kyQuantity;
							rc.itemBatch=JSON.stringify(dt);
									grid.updateCell('prodBSfquantity', rc.prodBSfquantity, manager.getRow(rc.__index));
									grid.updateCell('prodSfquantity', rc.prodSfquantity, manager.getRow(rc.__index));
									grid.updateCell('kyBQuantity', rc.kyBQuantity, manager.getRow(rc.__index));
									grid.updateCell('kyQuantity', rc.kyQuantity, manager.getRow(rc.__index));
									grid.updateCell('val', rc.val, manager.getRow(rc.__index));
									grid.updateCell('itemBatch',rc.itemBatch, manager.getRow(rc.__index));
									grid.updateRow(manager.getRow(rc.__index));
									this.content.Close();
							}else{
								this.content.Close();
							}
							
						}
						return false;
					},
					cancelVal : '关闭',
					cancel : true
				}).zindex();
	}
function ItemFreezedealWindow(rc,index) {
//  	if(undefined==$("#whcode").val() || ""==$("#whcode").val()){
// 			$.dialog.alert("请先选择仓库");
// 			return ;
// 	}
 	var manager = $("#table2").ligerGetGridManager();
		getDG().dialog(
				{
					title : '批次管理',
					width : 650,
					height : 350,
					content : 'url:' + sys_context+ '/yd/logic/soware/fzdItemManager.do?data='+encodeURIComponent(JSON.stringify(manager.getRow(rc.__index)))+'&billstatus=${soware.billstatus}'+'&updstatus='+rc.updstatus,//
					lock : true,
					max : true,
					min : true,
					ok : function() {
						this.content.yz();
						if(this.content.ba){
						var dt = this.content.grid.getData();
						if (dt != null) {
							rc.prodBSfquantity=this.content.jnum;
							rc.prodSfquantity=this.content.num;
							rc.val=this.content.num;
							rc.itemBatch=JSON.stringify(dt);
							rc.updstatus=1;
							grid2.updateCell('prodBSfquantity', rc.prodBSfquantity, manager.getRow(rc.__index));
							grid2.updateCell('prodSfquantity', rc.prodSfquantity, manager.getRow(rc.__index));
							grid2.updateCell('updstatus', rc.updstatus, manager.getRow(rc.__index));
							grid2.updateCell('val', rc.val, manager.getRow(rc.__index));
							grid2.updateCell('itemBatch',rc.itemBatch, manager.getRow(rc.__index));
							grid2.updateRow(manager.getRow(rc.__index));
									this.content.Close();
							}else{
								this.content.Close();
							}
							
						}
						return false;
					},
					cancelVal : '关闭',
					cancel : true
				}).zindex();
	}
</script>
</head>
<body>
	<form id="inputForm" action="" method="post">
		<input type="hidden" id="id" name="id" value="${soware.id}" />
		<input type="hidden" id="version" name="version" value="${soware.version}" />
		<input type="hidden" id="itemdata" name="itemdata" value="" /> 
		<input type="hidden" id="itemdata2" name="itemdata2" value="" /> 
		<input type="hidden" id="dataItem" name="dataItem"/> <!-- 列表产品单位json -->
<!-- 		<input type="hidden" id="batchItem" name="batchItem"/>列表产品批次json -->
<!-- 		<input type="hidden" id="yfitemdata" name="yfitemdata" value=""/> -->
		<input type="hidden" id="paramflag" name="paramflag" value="" />
		<!-- 用于标识提交按钮 -->
		<input type="hidden" id="submitFlag" value="" />
		
		<input type="hidden" id="exchangeRate" name="exchangeRate" value="${soware.exchangeRate}" /><!-- 汇率 -->
		<input type="hidden" id="currency" name="currency" value="${soware.currency}"/><!-- 币别 -->
		<div class="pageContent">
			<div class="panel">
				<table class="TableBlock" align="center">
				<tr id="tr1">
					<td nowrap="nowrap">销售出库编号:</td>
					<td ><input type="text" class="inp_width_98p" id="no" name="no" value="${soware.no}" readonly="readonly"/>
					</td>
					<td nowrap="nowrap">建单日期:</td>
   		            <td><input type="text" id="dctime" name="dctime" class="inp_width_98p" 
							value="<fmt:formatDate value="${empty soware.dctime?nowDate:soware.dctime}" type="both" dateStyle="long" pattern="yyyy-MM-dd" />" readonly="readonly"/>
					</td>
				</tr>
				<tr>
					<td nowrap="nowrap">销售订单编号:</td>
					<td>
					<c:choose>
						<c:when test="${'ADD' eq flag}">
						<select id="soNo" name="soNo" class="inp_width_98p required">
							<option> </option>
							<c:forEach items="${solist}" var="sodo">
								<option value="${sodo.no}">${sodo.no}</option>
							</c:forEach>
						</select>
						<script type="text/javascript">
   		                	$("#soNo").val("${soware.soNo}");
   		                </script>
						</c:when>
						<c:otherwise>
						<input type="text" value="${soware.soNo}" id="soNo" name="soNo" class="inp_width_98p required" readonly="readonly"></input>
						</c:otherwise>
					</c:choose>
   		            </td>
   		            <td nowrap="nowrap">购货单位名称:</td>
					<td>
						<input type="text" value="${soware.cliName}" id="cliName" name="cliName" class="inp_width_98p required" readonly="readonly"></input>
						<input type="hidden" value="${soware.cliNo}" id="cliNo" name="cliNo" readonly="readonly"></input>
					</td>
					
				</tr>
				<tr>
					<td nowrap="nowrap">购货单位地址:</td>
					<td colspan="3"><input type="text" value="${soware.cliAddr}" id="cliAddr" name="cliAddr" class="inp_width_98p "></input></td>
				</tr>
				<tr>
					<td nowrap="nowrap">出货仓库:</td>
					<td >
						<c:choose>
   		           		<c:when test="${'ADD' eq flag or '0'eq soware.billstatus or '3'eq soware.billstatus}">
							<select id="storage" name="storage" class="inp_width_98p required">
   		                 	<option></option>
   		                 	<c:forEach items='${wh}' var='state'>
   		                 	<option value='${state.code}'>${state.name}</option>
   		                 	</c:forEach>
   		                 	</select>
   		                 	<script type="text/javascript">
   		                		$("#storage").val("${soware.whcode}");
   		                 	</script>
   		                 	
   		                </c:when>
						<c:otherwise>
						<input type="text" value="${soware.whname}" id="storage" name="storage" class="inp_width_98p required" readonly="readonly"></input>
						</c:otherwise>
						</c:choose>
						
						<input type="hidden" id="whcode" name="whcode" value="${soware.whcode}"/>
   		                <input type="hidden" id="whname" name="whname" value="${soware.whname}"/>
					</td>
					<td nowrap="nowrap">交货时间:</td>
					<td >
						<input type="text" id="deliveryDate" name="deliveryDate" onclick="WdatePicker({dateFmt:'yyyy-MM-dd'})" class="inp_width_98p Wdate" value="<fmt:formatDate value="${soware.deliveryDate}" pattern="yyyy-MM-dd" type="both" />" readonly="readonly" /> 
					</td>
				</tr>
				<tr>
					<td nowrap="nowrap">制单人:</td>
   		           	<td><input type="text" class="inp_width_98p" id="listerName" name="listerName" value="${empty soware.listerName?user.realName:soware.listerName}" readonly="readonly"></input></td>
   		           	<td nowrap="nowrap">制单人部门:</td>
   		           	<td><c:choose>
   		           		<c:when test="${'ADD' eq flag or '0'eq soware.billstatus or '3'eq soware.billstatus}">
   		           		<select id="listerDeptName" name="listerDeptName" class="inp_width_98p">
								<option></option>
								<c:forEach items="${depts }" var="item">
									<option value="${item.depname }">${item.depname}</option>
								</c:forEach>
						</select> 
   		           			<script type="text/javascript">
   		           				$("#listerDeptName").val("${soware.listerDeptName}");
   		           		</script>
   		           		</c:when>
						<c:otherwise>
						<input type="text" value="${soware.listerDeptName}" id="listerDeptName" name="listerDeptName" class="inp_width_98p" readonly="readonly"></input>
						</c:otherwise>
						</c:choose>
   		           	</td>
   		        </tr>
				<tr>
					<td nowrap="nowrap">备注:</td>
					<td colspan="3"><input type="text" class="inp_width_98p" id="remark" name="remark" value="${soware.remark}" /></td>
				</tr>
				</table>
				<div id="table1"> </div> 
				
				<div id="table2"> </div> 
			</div>
			<div class="formBar">
				<ul>
					<li><div class="buttonActive">
							<div class="buttonContent">
								<button type="button" id="btn_fb_save" onclick="submitForm(0)">保存</button>
							</div>
						</div></li>
					<li><div class="buttonActive">
							<div class="buttonContent">
								<button type="button" id="btn_fb_approve_2" onclick="submitForm(1)">提交</button>
							</div>
						</div></li>
						
					<webres:displaycontroller elementId="LOGIC_SOWARE_2WANJIE" description="销售出库二级完结">
					<li><div class="buttonActive">
								<div class="buttonContent">
									<button type="button" id="btn_fb_end_2" onclick="submitFormEnd(2)" title="二级完结">审核</button>
								</div>
						</div></li>
					</webres:displaycontroller>
					<webres:displaycontroller elementId="LOGIC_SOWARE_2BOHUI" description="销售出库二级驳回">
					<li><div class="buttonActive">
								<div class="buttonContent">
									<button type="button" id="btn_fb_rebut_2" onclick="submitFormRebut(3);" title="二级驳回">驳回</button>
								</div>
						</div></li>
					</webres:displaycontroller>
					
					<li><div class="button">
							<div class="buttonContent">
								<button type="button" id="btn_fb_exportExcel" class="export"
									onclick="exportExcel()">导出</button>
							</div>
						</div></li>
					<li><div class="button">
							<div class="buttonContent">
								<button type="button" class="print" onclick="CloseDialog()">关闭</button>
							</div>
						</div></li>
				</ul>
			</div>
		</div>
		
	</form>
</body>
</html>

